package com.crowntit.types.design.framework.tree;

import lombok.Getter;
import lombok.Setter;

/**
 * 路由，决定当前情况走什么策略
 * 策略可以理解为大业务里的小服务，link里的node是策略里的逐步流程
 */
public abstract class AbstractStrategyRouter<T, D, R> implements StrategyHandler<T, D, R>, StrategyMapper<T, D, R> {

    @Setter
    @Getter
    protected StrategyHandler<T, D, R> defaultHandler = StrategyHandler.DEFAULT;

    public R router(T requestParameter, D dynamicContext) throws Exception{
        StrategyHandler<T, D, R> handler = get(requestParameter, dynamicContext);
        if (handler != null) return handler.apply(requestParameter, dynamicContext);
        return defaultHandler.apply(requestParameter, dynamicContext);
    }
}
